package com.cdsz.boot.workspace.biz.modules.acti.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cdsz.boot.biz.modules.system.entity.SysPosition;
import com.cdsz.boot.biz.modules.system.entity.SysUserPosition;
import com.cdsz.boot.biz.modules.system.service.ISysUserPositionService;
import com.cdsz.boot.workspace.biz.modules.acti.entity.UserPositionInfo;
import com.google.common.collect.Maps;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.cdsz.boot.workspace.biz.modules.acti.entity.PublicSetting;
import com.cdsz.boot.workspace.biz.modules.acti.service.IPublicSettingService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 公共设置
 * @Author: jeecg-boot
 * @Date:   2024-06-05
 * @Version: V1.0
 */
@Api(tags="公共设置")
@RestController
@RequestMapping("/acti/publicSetting")
@Slf4j
public class PublicSettingController extends JeecgController<PublicSetting, IPublicSettingService> {
	@Autowired
	private IPublicSettingService publicSettingService;

	@Autowired
	private ISysUserPositionService userPositionService;
	
	/**
	 * 分页列表查询
	 *
	 * @param publicSetting
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "公共设置-分页列表查询")
	@ApiOperation(value="公共设置-分页列表查询", notes="公共设置-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<PublicSetting>> queryPageList(PublicSetting publicSetting,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PublicSetting> queryWrapper = QueryGenerator.initQueryWrapper(publicSetting, req.getParameterMap());
		Page<PublicSetting> page = new Page<PublicSetting>(pageNo, pageSize);
		IPage<PublicSetting> pageList = publicSettingService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param publicSetting
	 * @return
	 */
	@AutoLog(value = "公共设置-添加")
	@ApiOperation(value="公共设置-添加", notes="公共设置-添加")
	//@RequiresPermissions("com.cdsz.boot.workspace.biz.modules:public_setting:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody PublicSetting publicSetting) {
		publicSettingService.save(publicSetting);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param publicSetting
	 * @return
	 */
	@AutoLog(value = "公共设置-编辑")
	@ApiOperation(value="公共设置-编辑", notes="公共设置-编辑")
	//@RequiresPermissions("com.cdsz.boot.workspace.biz.modules:public_setting:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody PublicSetting publicSetting) {
		publicSettingService.updateById(publicSetting);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "公共设置-通过id删除")
	@ApiOperation(value="公共设置-通过id删除", notes="公共设置-通过id删除")
	//@RequiresPermissions("com.cdsz.boot.workspace.biz.modules:public_setting:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		publicSettingService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "公共设置-批量删除")
	@ApiOperation(value="公共设置-批量删除", notes="公共设置-批量删除")
	//@RequiresPermissions("com.cdsz.boot.workspace.biz.modules:public_setting:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.publicSettingService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "公共设置-通过id查询")
	@ApiOperation(value="公共设置-通过id查询", notes="公共设置-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<PublicSetting> queryById(@RequestParam(name="id",required=true) String id) {
		PublicSetting publicSetting = publicSettingService.getById(id);
		if(publicSetting==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(publicSetting);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param publicSetting
    */
    //@RequiresPermissions("com.cdsz.boot.workspace.biz.modules:public_setting:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PublicSetting publicSetting) {
        return super.exportXls(request, publicSetting, PublicSetting.class, "公共设置");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("public_setting:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PublicSetting.class);
    }

	 /**
	  * 员工已有和未有的职务列表，穿梭框模式
	  *
	  * @param userId
	  * @return
	  */
	@GetMapping("/getUserHavePosition")
	 public Result<?> getUserHavePosition(String userId){
		List<SysPosition> userHavePositionList = publicSettingService.getUserHavePosition(userId);
		List<SysPosition> userNoHavepositionList = publicSettingService.getUserNoHavePosition(userId);
		Map<String,Object> ret = Maps.newHashMap();
		ret.put("userHavePositionList",userHavePositionList);
		ret.put("userNoHavepositionList",userNoHavepositionList);
		return Result.OK(ret);
	}

	 @RequestMapping(value = "/refreshUserPosition", method = RequestMethod.POST)
	 public Result<?> refreshUserPosition(HttpServletRequest request, HttpServletResponse response,@RequestBody UserPositionInfo userPositionInfo){
//		 System.out.println(userPositionInfo);
		//先全部删除
		QueryWrapper<SysUserPosition> userPositionQueryWrapper = new QueryWrapper<>();
		userPositionQueryWrapper.lambda().eq(SysUserPosition::getUserId,userPositionInfo.getUserId());
		userPositionService.remove(userPositionQueryWrapper);

		//再添加
		 List<SysUserPosition> userPositions = new ArrayList<>();
		 for(int i=0;i<userPositionInfo.getPositionIds().size();i++){
			 SysUserPosition userPosition = new SysUserPosition();
			 userPosition.setUserId(userPositionInfo.getUserId());
			 userPosition.setPositionId(userPositionInfo.getPositionIds().get(i));
			 userPositions.add(userPosition);
		 }
		 userPositionService.saveBatch(userPositions);
		return Result.ok("职位更新完毕");
	 }

}
